package com.glink.gateway.common;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : qiushaoshan
 * @create 2024/6/26 9:48
 */
@Data
@Component
@ConfigurationProperties(prefix = "anti.replay")
public class ReplayProperties {

    /**
     * 是否启用接口签名验证
     */
    private Boolean enabled = true;

    /**
     * Request Header信息对象
     */
    private HeaderKey headerKey = new HeaderKey();
    /**
     * 请求配置
     */
    private Request request = new Request();
    /**
     * 缓存配置
     */
    private Cache cache = new Cache();

    @Data
    public static class HeaderKey {
        /**
         * 请求ID 防止重放
         */
        private String nonce = "requestId";
        /**
         * 请求时间 避免缓存时间过后重放
         */
        private String timestamp = "timestamp";
        /**
         * 签名摘要
         */
        private String signData = "signData";
    }

    @Data
    public static class Request {
        /**
         * 请求有效期
         */
        private Long expireTime = 60L * 1000L * 5L;
    }

    @Data
    public static class Cache {
        /**
         * 缓存Key前缀
         */
        private String cacheKeyPrefix = "security:gateway:";
        /**
         * 锁持续时间(避免异常造成锁不释放)
         */
        private long lockHoldTime = 30L;
    }

    public static ReplayProperties props() {
        return SpringContextUtil.getBean(ReplayProperties.class);
    }
}
